package com.homework4;

import java.lang.reflect.Field;

/**
 * @author :  DongZheng Wang
 * @PROJECT_NAME: java泛型
 * @date :  2022-10-27 09:40
 **/
public class Mytest1 {
    public static void main(String[] args) throws IllegalAccessException, InstantiationException, NoSuchFieldException {
        //通过类名直接获取
        Class mytest2Class = Mytest2.class;
        System.out.println(mytest2Class);
        //通过对象名直接获取
        Mytest2 mytest2 = new Mytest2();
        Class aClass = mytest2.getClass();
        System.out.println(aClass);
        //通过类名去获取
        Class aClass1 = null;
        try {
            aClass1 = Class.forName("com.homework4.Mytest2");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        System.out.println(aClass1);
        Class<Mytest4> mytest4Class = Mytest4.class;
        Mytest4 mytest4 = mytest4Class.newInstance();
        Mytest4 mytest5 = mytest4Class.newInstance();
        System.out.println(mytest5==mytest4);

//        System.out.println(mytest4Class.getField("a"));
//        for (Field field : mytest4Class.getFields()) {
//            System.out.println(field);
//        }
        Field b = mytest4Class.getField("b");
        b.set(mytest4,12);
        b.get(mytest4);
        System.out.println(mytest4);


        Field a = mytest4Class.getField("a");
        a.setAccessible(true);
        a.set(mytest4,12);
        a.get(mytest4);
        System.out.println(mytest4);

    }




}
class Mytest2 {
    private int a;
    private int b;

    public void Mytest3() {
        System.out.println("~~~~~~~~~~");
    }
}
 class Mytest4{
    private int a;
    public int b;
     public void Mytest3(){
         System.out.println("~~~~~~~~~~");
     }

     @Override
     public String toString() {
         return "Mytest4{" +
                 "a=" + a +
                 ", b=" + b +
                 '}';
     }
 }

